home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / wstr.exe / WMISC.C < prev    next >
Text File  |  1993-02-25  |  5KB  |  228 lines

  1. #include <dos.h>
  2. #include <string.h>
  3. #include <WMisc.h>
  4. #include <WDOS.h>
  5. #pragma hdrstop
  6.  
  7. // copyright (c) 1992, 1993 by Paul Wheaton
  8.  
  9. //.parse
  10.  
  11. #ifndef MAJORBBS
  12.  
  13.   void Sound(float Pitch,int MilliSecs)
  14.     {
  15.       sound(int(Pitch));
  16.       Delay(MilliSecs);
  17.       nosound();
  18.     }
  19.  
  20. #endif
  21.  
  22. SByte  Abs(SByte  x)  { return x >= 0 ? x : -x; }
  23. SWord  Abs(SWord  x)  { return x >= 0 ? x : -x; }
  24. int    Abs(int    x)  { return x >= 0 ? x : -x; }
  25. SLong  Abs(SLong  x)  { return x >= 0 ? x : -x; }
  26. float  Abs(float  x)  { return x >= 0 ? x : -x; }
  27. double Abs(double x)  { return x >= 0 ? x : -x; }
  28.  
  29. //.parse
  30.  
  31. Byte   Max(Byte   a, Byte b  ) { return a >= b ? a : b; }
  32. SByte  Max(SByte  a, SByte b ) { return a >= b ? a : b; }
  33. Word   Max(Word   a, Word b  ) { return a >= b ? a : b; }
  34. SWord  Max(SWord  a, SWord b ) { return a >= b ? a : b; }
  35. int    Max(int    a, int b   ) { return a >= b ? a : b; }
  36. UInt   Max(UInt   a, UInt b  ) { return a >= b ? a : b; }
  37. Long   Max(Long   a, Long b  ) { return a >= b ? a : b; }
  38. SLong  Max(SLong  a, SLong b ) { return a >= b ? a : b; }
  39. double Max(double a, double b) { return a >= b ? a : b; }
  40.  
  41. //.parse
  42.  
  43. Byte   Min(Byte   a, Byte b  ) { return a <= b ? a : b; }
  44. SByte  Min(SByte  a, SByte b ) { return a <= b ? a : b; }
  45. Word   Min(Word   a, Word b  ) { return a <= b ? a : b; }
  46. SWord  Min(SWord  a, SWord b ) { return a <= b ? a : b; }
  47. int    Min(int    a, int b   ) { return a <= b ? a : b; }
  48. UInt   Min(UInt   a, UInt b  ) { return a <= b ? a : b; }
  49. Long   Min(Long   a, Long b  ) { return a <= b ? a : b; }
  50. SLong  Min(SLong  a, SLong b ) { return a <= b ? a : b; }
  51. double Min(double a, double b) { return a <= b ? a : b; }
  52.  
  53. //.parse
  54.  
  55. Bool InRange(Byte   Val, Byte   Low, Byte   High)
  56.   {return((Val>=Low) && (Val<=High));}
  57. Bool InRange(SByte  Val, SByte  Low, SByte  High)
  58.   {return((Val>=Low) && (Val<=High));}
  59. Bool InRange(Word   Val, Word   Low, Word   High)
  60.   {return((Val>=Low) && (Val<=High));}
  61. Bool InRange(SWord  Val, SWord  Low, SWord  High)
  62.   {return((Val>=Low) && (Val<=High));}
  63. //.parse
  64.  
  65. Bool InRange(int    Val, int    Low, int    High)
  66.   {return((Val>=Low) && (Val<=High));}
  67. Bool InRange(UInt   Val, UInt   Low, UInt   High)
  68.   {return((Val>=Low) && (Val<=High));}
  69. Bool InRange(Long   Val, Long   Low, Long   High)
  70.   {return((Val>=Low) && (Val<=High));}
  71. //.parse
  72.  
  73. Bool InRange(SLong  Val, SLong  Low, SLong  High)
  74.   {return((Val>=Low) && (Val<=High));}
  75. Bool InRange(double Val, double Low, double High)
  76.   {return((Val>=Low) && (Val<=High));}
  77. Bool InRange(long double Val, long double Low, long double High)
  78.   {return((Val>=Low) && (Val<=High));}
  79.  
  80. //.parse
  81.  
  82. Long SystemTick()
  83.   {
  84.     Registers Regs;
  85.     Regs.AH()=0;
  86.     CallBIOS(26,Regs);
  87.     return(Regs.CX()*MaxWord+Regs.DX());
  88.   }
  89.  
  90. //.parse
  91.  
  92. SLong Round(double Val)
  93.   {
  94.     return SLong((Val<0.0)?(Val-0.5):(Val+0.5));
  95.   }
  96.  
  97. //.parse
  98.  
  99. SLong RoundUp(double Val)
  100.   {
  101.     return SLong((Val<0.0)?(Val):(Val+1.0));
  102.   }
  103.  
  104. SLong RoundDown(double Val)
  105.   {
  106.     return SLong((Val<0.0)?(Val-1.0):(Val));
  107.   }
  108.  
  109. //.parse
  110.  
  111. SLong RoundOut(double Val)
  112.   {
  113.     return SLong((Val<0.0)?(Val-1.0):(Val+1.0));
  114.   }
  115.  
  116. SLong RoundIn(double Val)
  117.   {
  118.     SLong X=SLong(Val);
  119.     return X;
  120.   }
  121.  
  122. //.parse
  123.  
  124. char ToUpper(char C)
  125.   {
  126.     return (InRange(C,'a','z')?C-32:C);
  127.   }
  128.  
  129. char ToLower(char C)
  130.   {
  131.     return (InRange(C,'A','Z')?C+32:C);
  132.   }
  133.  
  134. /*  a pigeon hole algorithm (I think):
  135.  
  136.        Tries to spread a number as evenly as possible across an array
  137. */
  138.  
  139. //.parse
  140.  
  141. void PigeonHole(Long TotQuan, int ArrayLen, int A[])
  142.   {
  143.     int I;
  144.     int V=int(TotQuan/ArrayLen);
  145.     For(I,ArrayLen) A[I]=V;
  146.     if (TotQuan%ArrayLen==0) return;
  147.     double X= -0.5;  //  an accumulator
  148.     int Left=int(TotQuan-(V*ArrayLen)); // amount left
  149.     double D=double(Left)/ArrayLen;
  150.       // difference between the int val there is and what the float value s/b
  151.     int Stop=ArrayLen-1; // I wanna do the last one seperately
  152.     For(I,Stop)
  153.       {
  154.         X+=D;
  155.         if (X>=0.0)
  156.           {
  157.             A[I]++;
  158.             X-=1.0;
  159.             Left--;
  160.           }
  161.       }
  162.     A[Stop]+=Left;
  163.       // this makes certain that all of TotQuan is spread out.
  164.       // leaves no room for floating point round off probs
  165.   }
  166.  
  167. //.parse
  168.  
  169. const Word CRCMask=0x1021; // crc-ccitt mask
  170.  
  171. Word CRC(void* P,Long Size)
  172.   {
  173.     Word X=0;
  174.     char* CP=(char*)P;
  175.     Long I;
  176.     For(I,Size)
  177.       {
  178.         Word Z=Word(*CP);
  179.         CP++;
  180.         Z<<=8;
  181.         Word J;
  182.         For(J,8)
  183.           {
  184.             if((X ^ Z) & 0x8000) X=(X<<1)^CRCMask;
  185.             else X<<=1;
  186.             Z<<=1;
  187.           }
  188.       }
  189.     return X;
  190.   }
  191.  
  192. Word CRC(const char* S)
  193.   {
  194.     return (CRC((void*)S,Long(strlen(S))));
  195.   }
  196.  
  197. //.parse
  198.  
  199. void SimpleEncode(char* S)
  200.   {
  201.     int Size=strlen(S);
  202.     Word I;
  203.     For(I,Size) S[I]-=20;
  204.   }
  205.  
  206. void SimpleDecode(char* S)
  207.   {
  208.     int Size=strlen(S);
  209.     int I;
  210.     For(I,Size) S[I]+=20;
  211.   }
  212.  
  213. //.parse
  214.  
  215. #ifndef MAJORBBS
  216.  
  217.   void Beep(int Num)
  218.     {
  219.       int I;
  220.       For(I,Num)
  221.         {
  222.           Sound(783.99,35); //  note G5 for 0.05 secs
  223.           Sound(1174.66,35); // note D6
  224.         }
  225.     }
  226.  
  227. #endif
  228.